package newx.com.control.scroll.text
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	
	public class NxNewScrollBar extends Sprite
	{
		protected var arrowUp:SimpleButton;
		protected var arrowDown:SimpleButton;
		protected var track:MovieClip;
		protected var thoumb:MovieClip;
		
		protected var ratio:Number;		
		protected var ratioMax:int;
		
		//可视区域的高度
		protected var showRectHeight:int =100;
		//当前文本内容的高度
		protected var textFileHeight:int = 100; 
		
		private var btnWH:int				=	16;
		
		protected static var move:int = 1; 
		
		protected var _callback:Function;
		
		private var inited:Boolean;
				
		public function NxNewScrollBar()
		{			
			ratio = 0;
			inited = false;
			Init();
		}
		
		private function Init():void
		{
			var scrollSkin:MovieClip = ResManager.newResClass("vScrol_1") as MovieClip;
			arrowUp = (scrollSkin.arrow1 as SimpleButton);
			arrowUp.x = -1;
			arrowUp.y = 0;	
//			arrowUp.height	=	arrowUp.width	=	btnWH;
			arrowUp.addEventListener(MouseEvent.MOUSE_DOWN, thoumbUp);
			arrowUp.addEventListener(MouseEvent.ROLL_OUT,stopScroll);
			arrowUp.addEventListener(MouseEvent.MOUSE_UP, stopScroll);
			
			track = (scrollSkin.track as MovieClip);
			track.x = 0;
			track.y = arrowUp.height
			track.height = showRectHeight - btnWH * 2
			
			arrowDown = (scrollSkin.arrow2 as SimpleButton);
//			arrowDown.height	=	arrowDown.width	=	btnWH;
			arrowDown.x = -1;
			arrowDown.y = track.y + track.height
			arrowDown.addEventListener(MouseEvent.MOUSE_DOWN, thoumbDown);
			arrowDown.addEventListener(MouseEvent.MOUSE_UP, stopScroll);
			arrowDown.addEventListener(MouseEvent.ROLL_OUT,stopScroll);
			
			thoumb = (scrollSkin.thoumb as MovieClip);
			thoumb.x = 0;
			thoumb.y = track.y
			thoumb.gotoAndStop(1);
			thoumb.buttonMode = true;
			thoumb.addEventListener(MouseEvent.MOUSE_DOWN, thoumbStartDrag);
			thoumb.addEventListener(MouseEvent.ROLL_OUT, onOut);
			thoumb.addEventListener(MouseEvent.ROLL_OVER, onOver);
			
			addChild(arrowUp);
			addChild(track);
			addChild(arrowDown);
			addChild(thoumb);
			
			addEventListener(MouseEvent.MOUSE_WHEEL, thoumbMouseWheel);
			
			inited = true;
			setThoumbPos();
			resize();
		}
		
		private function thoumbUp(e:MouseEvent):void			
		{
			addEventListener(Event.ENTER_FRAME, goUp);
		}
		
		private function goUp(e:Event):void
		{			
			ratio -= move;
			if(ratio < 0)
			{
				ratio = 0;
			}
			setThoumbPos();			
		}
		
		private function thoumbDown(e:MouseEvent):void
		{
			addEventListener(Event.ENTER_FRAME, goDown);
		}

		private function goDown(e:Event):void
		{
			ratio += move;
			if(ratio > ratioMax)
			{
				ratio = ratioMax;
			}
			setThoumbPos();
		}
		
		private function stopScroll(e:MouseEvent):void
		{
			removeEventListener(Event.ENTER_FRAME, goDown);
			removeEventListener(Event.ENTER_FRAME, goUp);
		}
		
		private function setThoumbPos():void
		{
			if (inited)
			{
				var tempY:Number	=	track.height * (Ratio / textFileHeight) +	track.y 
				if (tempY > track.y +	track.height - thoumb.height) 
				{
					tempY	=	track.y +	track.height - thoumb.height;
				}
				else if (tempY < track.y)
				{
					tempY = track.y
				}
				thoumb.y =  tempY
			}
			if (_callback != null)
			{
				_callback.call();
			}
		}
		
		private function thoumbStartDrag(e:MouseEvent):void
		{
			thoumb.gotoAndStop(3);
			
			thoumb.startDrag(false, new Rectangle(0,btnWH,0,track.height-thoumb.height));
			addEventListener(Event.ENTER_FRAME, checkThounmb);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, thoumbStopDrag);
		}
		
		private function onOut(e:MouseEvent):void
		{
			thoumb.gotoAndStop(1);
		}
		
		private function onOver(e:MouseEvent):void
		{
			thoumb.gotoAndStop(2);
		}
		
		private function thoumbStopDrag(e:MouseEvent):void
		{
			thoumb.gotoAndStop(1);
			thoumb.stopDrag();
			removeEventListener(Event.ENTER_FRAME, checkThounmb);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, thoumbStopDrag);
		}
		
		private function checkThounmb(e:Event):void
		{
			ratio =int( (thoumb.y -track.y) / (track.height - thoumb.height) * (textFileHeight - showRectHeight));
			if(_callback != null)
				_callback.call();
		}
		
		private function thoumbMouseWheel(e:MouseEvent):void
		{
			ratio -= e.delta;
			if(ratio > ratioMax)
			{
				ratio = ratioMax;
			}
			if(ratio < 0)
			{
				ratio = 0;
			}
			setThoumbPos();
		}
		
		public function set Ratio(val:Number):void
		{
			ratio = val;
			setThoumbPos();
		}
		
		public function get Ratio():Number
		{
			return ratio;
		}
		
		public function setHeight(h:int=50, sh:int=50):void
		{	
			if(h < 50)
			{
				showRectHeight = 50;
			}
			else
			{
				showRectHeight = h;
			}
			if(sh < 50)
			{
				textFileHeight = 50;
			}
			else
			{
				textFileHeight = sh;
			}
			ratioMax	=	textFileHeight	-	showRectHeight;
			if(textFileHeight <= showRectHeight)
			{				
				this.visible = false;
			}
			else
			{
				this.visible = true;
				resize();
			}
		}
		
		private function resize():void
		{
			if(!inited)
				return;
			
			track.height = showRectHeight - btnWH * 2;
			
			arrowDown.x = 0;
			arrowDown.y = track.y + track.height;
			
			thoumb.x = 0;
			thoumb.y = btnWH;
			var th:Number =  track.height *(showRectHeight/textFileHeight);
			if(th < btnWH)
			{
				th = btnWH;
			}
			else if(th > track.height)
			{
				th = track.height;
			}
			thoumb.height = th;
			setThoumbPos();
		}
		
		public function bind(val:Function):void
		{
			_callback = val;
		}
	}
}